package cn.iot.util;

import cn.iot.common.dto.ResultMsg;
import com.netsdk.demo.module.GateModule;
import com.netsdk.demo.module.LoginModule;
import com.netsdk.lib.NetSDKLib;
import com.netsdk.lib.ToolKits;

public class DaHuaUtil {
    /**
     * 修改用户时段规则
     * @param cardNo
     * @param timeScheduleNo
     * @return
     */
    public ResultMsg modifyCardTime(String cardNo,int timeScheduleNo){
        boolean init = LoginModule.init(null, null);
        boolean isLogin = LoginModule.login("192.168.1.198", 37777, "admin", "Admin123456");
        if (!isLogin) {
            return new ResultMsg(500, "登录失败", null);
        }
        boolean isCardFind = GateModule.findCard(cardNo);
        if (!isCardFind) {
            return new ResultMsg(500, "卡信息不存在", null);
        }
        //卡片存在
        NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARD[] recordset_access_ctl_cards = GateModule.findNextCard(1);
        NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARD accessCardInfo = recordset_access_ctl_cards[0];
        //   NetSDKLib.NET_RECORDSET_ACCESS_CTL_CARD accessCardInfo = GateModule.findNextCard(1)[0];
        if (accessCardInfo == null) {
            return new ResultMsg(500, "卡信息不存在", null);
        }
        //-- 设置开门权限
        accessCardInfo.nDoorNum = 2;
        accessCardInfo.sznDoors[0] = 0;
        accessCardInfo.sznDoors[1] = 1;
        accessCardInfo.nTimeSectionNum = 2;      // 与门数对应
        accessCardInfo.sznTimeSectionNo[0] = timeScheduleNo; // 表示第一个门全天有效
        accessCardInfo.sznTimeSectionNo[1] = timeScheduleNo; // 表示第二个门全天有效

        /**
         * 记录集操作
         */
        NetSDKLib.NET_CTRL_RECORDSET_PARAM update = new NetSDKLib.NET_CTRL_RECORDSET_PARAM();
        update.emType = NetSDKLib.EM_NET_RECORD_TYPE.NET_RECORD_ACCESSCTLCARD;    // 记录集信息类型
        update.pBuf = accessCardInfo.getPointer();

        accessCardInfo.write();
        update.write();
        boolean bRet = LoginModule.netsdk.CLIENT_ControlDevice(LoginModule.m_hLoginHandle,
                NetSDKLib.CtrlType.CTRLTYPE_CTRL_RECORDSET_UPDATE, update.getPointer(), 5000);
        update.read();
        accessCardInfo.read();
        if (!bRet) {
            System.err.println("修改卡信息失败." + ToolKits.getErrorCodePrint());
            return new ResultMsg(500, "修改卡信息失败", null);
        } else {
            System.out.println("修改卡信息成功 ");
        }
        LoginModule.logout();
        return new ResultMsg(200, "修改成功", null);
    }
}
